//window._MPEOPLE={
    window._MSAVEPEOPLE={

    _MAINLABEL:null,

    _PAGESIZE:15,

    _PAGEINDEX:0,

    showPeople:function(parentNode){

        var uiObj = _GRM.getNodeObj('saveTeam');
        var mainLabel = uiObj.node;

        _MSAVEPEOPLE._MAINLABEL = mainLabel;
        _MSAVEPEOPLE._PAGEINDEX = 0;

        if(uiObj.isnew){
            _MSAVEPEOPLE.initButton(mainLabel);
        }
        _MSAVEPEOPLE.refreshLeft();
        _MSAVEPEOPLE.refreshRight();
        parentNode.addChild(mainLabel);
        return mainLabel;

    },

    getArrByType:function(){

        var saveInfo = _GAMEDATAHELPER.getSaveTeam();

        if(saveInfo){
            return  saveInfo.arr;
        }

        return null;
    },

    refreshRight:function(){
        var pageNode = _MSAVEPEOPLE._MAINLABEL.getChildByName('rightLabel').getChildByName('background')
                                .getChildByName('page');

        var roleNodeArr = _MSAVEPEOPLE._MAINLABEL.getChildByName('rightLabel')
                                            .getChildByName('background')
                                            .getChildByName('goods').getChildren();
        
        

        var startIndex = _MSAVEPEOPLE._PAGEINDEX*_MSAVEPEOPLE._PAGESIZE;

        var userArr = _MSAVEPEOPLE.getArrByType();

        for(var i=0;i<_MSAVEPEOPLE._PAGESIZE;i++){
            _MSAVEPEOPLE.refreshRightOne(roleNodeArr[i],userArr[startIndex+i]);
        }
        pageNode.getChildByName('label').getComponent(cc.Label).string = "第"+(_MSAVEPEOPLE._PAGEINDEX+1)+"页";
    },

    refreshRightOne:function(node,roleInfo){

        if(roleInfo){

            node.active = true;

            var roleData = _ROLEDATA.getRoleDataOfPlayer(roleInfo);
            var lv = roleData.lv;
            var friendData = _GAMEDATAHELPER.getFriendValue(roleInfo.name);

            var faceNode = node.getChildByName('touxian');
            var nameNode = node.getChildByName('name');
            var lvNode = node.getChildByName('lv');
            var ftypeNode = node.getChildByName('qiu');
            var ftypeStrNode = ftypeNode.getChildByName('label');
            var friendNode = node.getChildByName('friend');
            var friendtitleNode = friendNode.getChildByName('title');

            faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(roleInfo.face);
            nameNode.getComponent(cc.Label).string = roleInfo.name;
            lvNode.getComponent(cc.Label).string = "LV."+lv;
            ftypeNode.color = new cc.color(_CS.ftype[roleInfo.ftype].color);
            ftypeStrNode.getComponent(cc.Label).string = roleInfo.ftype;
            friendtitleNode.getComponent(cc.Label).string = "友好:"+friendData;
            friendNode.getChildByName('progress').getComponent(cc.ProgressBar).progress = friendData/100;

        }else{
            node.active = false;
        }
        
    },

    refreshLeft:function(){

        var roleNodeArr = _MSAVEPEOPLE._MAINLABEL.getChildByName('leftLabel')
                                            .getChildByName('troops')
                                            .getChildByName('troops').getChildren();
        for(var i=0;i<roleNodeArr.length;i++){

            _MSAVEPEOPLE.refreshLeftOne(roleNodeArr[i],window.gameData.playerTeam[i]);

        }
    },

    refreshLeftOne:function(node,roleInfo){

        var imgAddNode = node.getChildByName('img_add');
        var informationNode = node.getChildByName('information');
        if(roleInfo){
            imgAddNode.active = false;
            informationNode.active = true;

            var faceNode = informationNode.getChildByName('face');
            var barsNode = informationNode.getChildByName('bars');
            var nameNode = faceNode.getChildByName('name');
            var levelNode = faceNode.getChildByName('level');
            var ftypebgNode = faceNode.getChildByName('属性bg');
            var ftypeNode = ftypebgNode.getChildByName('ftype');
            var expbarNode = barsNode.getChildByName('exp');
            var exptitleNode = expbarNode.getChildByName('title');
            var hpbarNode = barsNode.getChildByName('hp');
            var hptitleNode = hpbarNode.getChildByName('title');
            var magicbarNode = barsNode.getChildByName('angry');
            var magictitleNode = magicbarNode.getChildByName('title');
            var roleTypeNode = informationNode.getChildByName('roleType');

            var roleData = _ROLEDATA.getRoleDataOfPlayer(roleInfo);
            var maxHp = roleData.maxHp;
            var maxMagic = roleData.maxMagic;
            var expObj = _EXPMANAGER.getNextLevelExp(roleInfo.exp);

            levelNode.getComponent(cc.Label).string = "LV."+roleData.lv;
            nameNode.getComponent(cc.Label).string = roleInfo.name;
            ftypeNode.getComponent(cc.Label).string = roleInfo.ftype;
            faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(roleInfo.face);
            exptitleNode.getComponent(cc.Label).string = "经验:"+expObj.cexp+"/"+expObj.nexp;
            hptitleNode.getComponent(cc.Label).string = "生命:"+roleInfo.hp+"/"+maxHp;
            magictitleNode.getComponent(cc.Label).string = "魔法:"+roleInfo.mg+"/"+maxMagic;

            expbarNode.getChildByName('progress').getComponent(cc.ProgressBar).progress = expObj.cexp/expObj.nexp;
            hpbarNode.getChildByName('progress').getComponent(cc.ProgressBar).progress = roleInfo.hp/maxHp;
            magicbarNode.getChildByName('progress').getComponent(cc.ProgressBar).progress = roleInfo.mg/maxMagic;
            ftypebgNode.color = new cc.color(_CS.ftype[roleInfo.ftype].color);


            roleTypeNode.getChildByName('belong').active = false;
            roleTypeNode.getChildByName('custom').active = false;
            roleTypeNode.getChildByName('main').active = false;

        }else{
            imgAddNode.active = true;
            informationNode.active = false;
        }

    },
              

    initButton:function(mainLabel){

        var roleNodeArr =mainLabel.getChildByName('leftLabel')
                                    .getChildByName('troops')
                                    .getChildByName('troops').getChildren();
        var roleNodeArrRight = mainLabel.getChildByName('rightLabel')
                                    .getChildByName('background')
                                    .getChildByName('goods').getChildren();
        var btns = mainLabel.getChildByName('rightLabel').getChildByName('btn');
        var btnClose = btns.getChildByName('close');
        var btnTips = btns.getChildByName('tips');

        btnClose.on(cc.Node.EventType.TOUCH_START, function(){
            _MAP.refreshMapTeam();
            _GRM.putNode(this);
        }, mainLabel);

        btnTips.on(cc.Node.EventType.TOUCH_START, function(){
            _TIPS.showMessage("寄存角色不受重置修为影响且可以跨周目继承,不过必须全局修为满足才可以上阵");
        }, mainLabel);


        //正选按钮
        for(var i=0;i<roleNodeArr.length;i++){
            var node = roleNodeArr[i];
            var btnDown = node.getChildByName('information').getChildByName('buttonDown');
            var btnInfo = node.getChildByName('information').getChildByName('face');

            btnDown.nodeIndex = i;
            btnDown.on(cc.Node.EventType.TOUCH_START, function(){
                //_TIPS.showMessage('下阵'+this.nodeIndex,0);

                var role = window.gameData.playerTeam[this.nodeIndex];
                if(role){
                    if(window.gameData.playerTeam.length>1){
                        window.gameData.playerTeam.splice(this.nodeIndex,1);
                        if(!role.isCustomRole){
                            var userArr = _MSAVEPEOPLE.getArrByType();
                            userArr.unshift(role);
                        }else{
                            var npclog = _GAMEDATAHELPER.getNpcLog(role.name);
                            if(npclog){
                                npclog.diq=0;
                                npclog.gy=0;
                            }
                        }
                        _MSAVEPEOPLE.refreshLeft();
                        _MSAVEPEOPLE.refreshRight();
                    }else{
                        _TIPS.showMessage('上阵人数不能少于1!',0);
                    }
                }

            }, btnDown);

            btnInfo.nodeIndex = i;
            btnInfo.on(cc.Node.EventType.TOUCH_START, function(){
                //_TIPS.showMessage('详情'+this.nodeIndex,0);

                _MRDL.initUi(_MAP.node,{roleIndex:this.nodeIndex,
                                        sence:_STYPE._TEAMSENCE,
                                        roleArr:window.gameData.playerTeam});

            }, btnInfo);
        }

        //替补按钮
        for(var i=0;i<roleNodeArrRight.length;i++){
            var node = roleNodeArrRight[i];
            var nodebtns = node.getChildByName('btns');
            var btnUp = nodebtns.getChildByName('buttonUp');
            var btnInfo=node.getChildByName('touxian');
            var btnAction=nodebtns.getChildByName('buttonAction');

            btnUp.nodeIndex = i;
            btnUp.on(cc.Node.EventType.TOUCH_START, function(){
                var userArr = _MSAVEPEOPLE.getArrByType();
                var realIndex = _MSAVEPEOPLE._PAGESIZE*_MSAVEPEOPLE._PAGEINDEX+this.nodeIndex;
                var role = userArr[realIndex];
                if(role){
                    var maxlength = _GAMEDATAHELPER.getMaxLengthOfPlayerTeam();
                    if(window.gameData.playerTeam.length<maxlength){

                        var xwobj = _MSAVEPEOPLE.getXwOfRole(role);
                        var enoughxw = _MSAVEPEOPLE.isXwEnough(xwobj);
                        if(enoughxw.length<=0){
                            userArr.splice(realIndex,1);
                            window.gameData.playerTeam.push(role);
                            _MSAVEPEOPLE.refreshLeft();
                            _MSAVEPEOPLE.refreshRight();
                        }else{
                            _TIPS.showMessage('全局修为不满足:'+enoughxw.join(","));
                        }
                    }else{
                        _TIPS.showMessage('上阵人数不能大于'+maxlength);
                    }
                }
                
            }, btnUp);

            btnInfo.nodeIndex = i;
            btnInfo.on(cc.Node.EventType.TOUCH_START, function(){
                var userArr = _MSAVEPEOPLE.getArrByType();
                var realIndex = _MSAVEPEOPLE._PAGESIZE*_MSAVEPEOPLE._PAGEINDEX+this.nodeIndex;
                _MRDL.initUi(_MAP.node,{
                                        roleIndex:realIndex,
                                        sence:_STYPE._TEAMSENCE,
                                        roleArr:userArr});
            }, btnInfo);

            btnAction.nodeIndex = i;
            btnAction.on(cc.Node.EventType.TOUCH_START, function(){
                
                var realIndex = _MSAVEPEOPLE._PAGESIZE*_MSAVEPEOPLE._PAGEINDEX+this.nodeIndex;
                var userArr = _MSAVEPEOPLE.getArrByType();
                var role = userArr[realIndex];
                var xwObj = _MSAVEPEOPLE.getXwOfRole(role);
                _TIPS.showMessage(_MSAVEPEOPLE.getXwMessage(xwObj));
                console.log("修为",xwObj);
            }, btnAction);
        }
    },

    getXwOfRole:function(role){

        var xwobj = {};

        if(role){

            if(role.xw){
                for(var key in role.xw){
                    xwobj[key]=role.xw[key];
                }
            }


            if(role.job&&_JOB[role.job]){
                var newxwobj = _JOB[role.job].xw;
                for(var key in newxwobj){
                    if(!xwobj[key]){
                        xwobj[key]=newxwobj[key];
                    }else if(xwobj[key]<newxwobj[key]){
                        xwobj[key]=newxwobj[key];
                    }
                }
            }


            if(role.equip&&role.equip.length>0){
                for(var i=0;i<role.equip.length;i++){
                    var equipobj = role.equip[i];
                    var equipType = equipobj.type;
                    if(_EQUIP[equipType]&&_EQUIP[equipType].xw){
                        var newxwobj = _EQUIP[equipType].xw;
                        for(var key in newxwobj){
                            if(!xwobj[key]){
                                xwobj[key]=newxwobj[key];
                            }else if(xwobj[key]<newxwobj[key]){
                                xwobj[key]=newxwobj[key];
                            }
                        }
                    }
                }
            }


            if(role.skill&&role.skill.length>0){
                for(var i=0;i<role.skill.length;i++){
                    var skillname = role.skill[i];
                    var newxwobj = null;
                    if(_SKILL[skillname]){
                        newxwobj = _SKILL[skillname].xw;
                    }else if(_SKILLBD[skillname]){
                        newxwobj = _SKILLBD[skillname].xw;
                    }
                    for(var key in newxwobj){
                        if(!xwobj[key]){
                            xwobj[key]=newxwobj[key];
                        }else if(xwobj[key]<newxwobj[key]){
                            xwobj[key]=newxwobj[key];
                        }
                    }
                }
            }



        }

        return xwobj;

    },

    getXwMessage:function(xwobj){

        var message = "";

        for(var key in xwobj){

            message+=_XW[key].label+":"+xwobj[key]+" ";

        }

        return message;

    },

    isXwEnough:function(xwObj){

        var notEnoughArr = [];

        var xwlv = _MXLU.getXWLV();

        for(var key in xwObj){

            var value = xwObj[key];

            if(value>999){
                value=999;
            }

            var isEnough = false;
            for(var i=0;i<xwlv.length;i++){
                var tempObj = xwlv[i];
                if(tempObj.xw==key){
                    var realmax = tempObj.lv*100;
                    if(realmax>=value){
                        isEnough = true;
                    }
                }
            }

            if(!isEnough){
                if(value%100>0){
                    notEnoughArr.push(_XW[key].label+":"+(Math.floor(value/100)+1)+"级"); 
                }else{
                    notEnoughArr.push(_XW[key].label+":"+(Math.floor(value/100))+"级"); 
                }
            }

        }

        return notEnoughArr;

    }

}